package intranet.client;

/**
 * @author Baptiste RENDU
 */

import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Frame;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.PushButton;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

public class ViewerTab extends VerticalPanel{
	protected HorizontalPanel horizontalPanel;
	protected ListBox listBox;
	protected PushButton button;
	/**
	 * construit un objet ViewerTab heritant de VerticalPanel
	 */
	public ViewerTab()
	{
		
		horizontalPanel=new HorizontalPanel();
		listBox=new ListBox();
		listBox.setVisibleItemCount(1);
		listBox.setWidth("200px");
		listBox.addChangeHandler(new ChangeHandler() {
			@Override
			public void onChange(ChangeEvent event) {
				int index =((ListBox)event.getSource()).getSelectedIndex();
				remove(1);
				Frame f = new Frame("../Documents/"+((ListBox)event.getSource()).getValue(index));
				f.setSize(getOffsetWidth()+"px", (getOffsetHeight()-horizontalPanel.getOffsetHeight())+"px");
				add(f);
			}
		});
		button=new PushButton("x");
		button.setEnabled(false);
		button.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				listBox.removeItem(listBox.getSelectedIndex());	
				if(listBox.getItemCount()==0)
				{
					button.setEnabled(false);
					remove(1);
				}
				else
				{
					remove(1);
					Frame f = new Frame("../Documents/"+listBox.getValue(0));
					f.setSize(getOffsetWidth()+"px", (getOffsetHeight()-horizontalPanel.getOffsetHeight())+"px");
					add(f);
					listBox.setSelectedIndex(0);
				}
			}
		});
		horizontalPanel.add(new Label("Document "));
		horizontalPanel.add(listBox);
		horizontalPanel.add(button);
		add(horizontalPanel);
	}
	/**
	 * permet d'ajouter un document au panel
	 * @param le chemin du fichier sur le serveur et le nom du document
	 */
	public void addDocument(String path,String name)
	{
		if(listBox.getItemCount()==0)
		{
			button.setEnabled(true);
		}
		else
		{
			remove(1);
		}
		Frame f = new Frame("../Documents/"+path);
		f.setSize(getOffsetWidth()+"px", (getOffsetHeight()-horizontalPanel.getOffsetHeight())+"px");
		add(f);
		listBox.addItem(name,path);
		listBox.setSelectedIndex(listBox.getItemCount()-1);
		onResize();
	}
	/**
	 * fonction appelée pour redimensionner les objets affichés dans le widget
	 */
	public void onResize()
	{
		remove(1);
		Frame f = new Frame("../Documents/"+listBox.getValue(listBox.getSelectedIndex()));
		Widget w = getParent();
		f.setSize(w.getOffsetWidth()-30+"px", (w.getOffsetHeight()-100)+"px");
		add(f);
	}
}
